package com.example.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.example.controller.util.Code;
import com.example.controller.util.Data;
import com.example.domain.Activity;
import com.example.domain.Community;
import com.example.domain.User;
import com.example.service.CommunityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

@RestController
@RequestMapping("/community")
public class CommunityController {
    @Autowired
    private CommunityService communityService;
    @PostMapping
    public Data save(@RequestBody Community community){
        boolean save = communityService.save(community);
        Integer code = save ? Code.Code_ok : Code.Code_err;
        String msg = save ? "添加成功" : "添加失败";
        return new Data(code,msg);
    }

    /**
     * 社区界面展示(自定义显示条数，避免数据库)
     */
    @GetMapping
    public Data getAll(){
        //自定义社区显示条数
        int current=0;
        int size=8;
        IPage<Community> iPage = communityService.getCommnunity(current, size);
        Integer code = iPage!=null ? Code.Code_ok : Code.Code_err;
        String msg = iPage != null ? "querry_ok" : "querry_err";
        List<Community> listCom=new ArrayList<>();
        if (CollectionUtils.isEmpty(iPage.getRecords())){
            return null;
        }
        iPage.getRecords().forEach(record->{
            listCom.add(record);
        });
        System.out.println(listCom);
        return  new Data(code,iPage,msg);
    }

    @GetMapping("/page/{page}/{size}")
    public Data getPage(@PathVariable Integer page, @PathVariable Integer size) {
        IPage<Community> pageUser = communityService.getPageCommunity(page, size);
        Integer code = pageUser != null ? Code.Code_ok : Code.Code_err;
        String msg = pageUser != null ? "查询成功" : "查询失败";
        return new Data(code, pageUser, msg);
    }

    @GetMapping("/{id}")
    public Data getById(@PathVariable Integer id){
        Community community = communityService.getById(id);
        Integer code = community != null ? Code.Code_ok : Code.Code_err;
        String msg = community != null ? "查询成功" : "查询失败";
        return new Data(code, community, msg);
    }


    /**
     * 根据id删除社区动态
     * localhost：8080/commnunity/id
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public Data delete(@PathVariable Integer id){
        boolean b = communityService.removeById(id);
        Integer code = b==true ? Code.Code_ok : Code.Code_err;
        String msg = b==true ? "删除成功" : "删除失败";
        return new Data(code,msg);
    }
    @PutMapping()
    public Data updateById(@RequestBody Community community){
        boolean b = communityService.updateById(community);
        Integer code = b==true ? Code.Code_ok : Code.Code_err;
        String msg = b==true ? "更新成功" : "更新失败";
        return new Data(code,msg);
    }
}
